import { create } from 'zustand';
import { persist } from 'zustand/middleware';
// 初始值
const initialState = {
  userInfo: {},
};
// 初始化
const fn = (set) => {
  return {
    ...initialState,
    reset: () => set((state) => ({ ...initialState })), // 重置到初始值
    setUserInfo: (value) =>
      set((state) => {
        return { userInfo: value };
      }),
    removeUserInfo: () => set(() => ({ userInfo: {} })),
  };
};
// 存储名称
const options = {
  name: 'react-store',
  getStorage: () => localStorage,
};
// 持久化
const useStore = create(persist(fn, options));

//清空缓存
const clearStore = () => {
  useStore.persist.clearStorage();
};
// 导出
export { useStore, clearStore };
